// CGPS.cpp : Implementation of CCGPS
#include "stdafx.h"
#include "AddinGPS.h"
#include "CGPS.h"

#include  "HookFrame.h"
#include "HookView.h"
CList<CSubclassWnd*,CSubclassWnd*> CCGPS::m_SubWndList;
IFrameWork* CCGPS::m_pProjectFramework;
/////////////////////////////////////////////////////////////////////////////
// CCGPS
STDMETHODIMP CCGPS::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_ICGPS
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

HRESULT _stdcall CCGPS::Uninitialize( LONG lSessionID )
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())
		
		for(int i=0;i<m_SubWndList.GetCount();i++)
		{
			CSubclassWnd*pSubWnd = m_SubWndList.GetAt(m_SubWndList.FindIndex(i));
			delete pSubWnd;
		}
	return S_OK;	
	
}

HRESULT _stdcall CCGPS::EndInitialize()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())
		
	tagAddinManager addInfoManager;
	m_pProjectFramework->get_AddinfoManager(&addInfoManager);
	DWORD threadID = addInfoManager.m_dwMainThreadID;
	
	if(threadID==NULL)
		return S_FALSE;
	
	_bstr_t message = "GPS Addin Enum Subing window proc ";
	m_pProjectFramework->LoggingService(message);

	BOOL bSucceed = ::EnumThreadWindows(threadID,(WNDENUMPROC)EnumSubingWndProc,0);
	
	m_pProjectFramework ->GetAddinfo(m_lSessionID,&CHookView::m_pAddinfo);

	static tagAddinManager addinManager;
	m_pProjectFramework->get_AddinfoManager(&addinManager);
	CHookView::m_pAddinfoManager = &addinManager;

	if(!bSucceed)
		return S_FALSE;
	
	return S_OK;	
}

BOOL CALLBACK CCGPS::EnumSubingWndProc( HWND hwnd,LPARAM lParam )
{
	CString classname;
	GetClassName( hwnd, classname.GetBufferSetLength(200), 200 );
	classname.ReleaseBuffer();
	if(classname.Find("Afx" )  >=  0)
	{
		CHookFrame *pSubWnd = new CHookFrame();
		pSubWnd->HookWindow(hwnd);

		_bstr_t message = "GPS Addin Enum Subing window proc for Window {"+_bstr_t(classname)+"}";
		CCGPS::m_pProjectFramework->LoggingService(message);

		pSubWnd->SubChildCtrl();

		m_SubWndList.AddTail(pSubWnd);

	}
	return TRUE;	
	
}







STDMETHODIMP CCGPS::ShowMacroIDE()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here

	CHookView::m_spHost->ShowDebugger(VARIANT_TRUE);

	return S_OK;
}
